//
//  GetUpManagerViewController.m
//  拖小延闹钟
//
//  Created by rimi on 15/10/8.
//  Copyright (c) 2015年 Liukai. All rights reserved.
//

#import "GetUpManagerViewController.h"
#import "ClockTableViewCell.h"

@interface GetUpManagerViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UIButton *addButton;  //添加闹钟按钮
@property (nonatomic, strong) UITableView *tableView;   //tableview
@property (nonatomic, strong) NSMutableArray *dataSource;   //数据源
@property (nonatomic, strong) AddNewViewController *addVC;
@end
static NSString *identifier = @"ClockCell";
@implementation GetUpManagerViewController
- (void)viewWillAppear:(BOOL)animated {
}
- (void)viewDidLoad {
    self.theTitle = @"起床管家";
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initDatasouce];
    [self initUserinterface];
}
- (void)initDatasouce {
        self.dataSource = [[NSUserDefaults standardUserDefaults] objectForKey:@"datasource"];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshDatasource) name:@"deleteClock" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getClockNumber:) name:@"changeSetting" object:nil];
}
- (void)refreshDatasource {
    [self.tableView reloadData];
}

- (void)getClockNumber:(NSNotification *)sender {
    NSLog(@"idx = %@",sender.userInfo[@"idx"]);
    [self refreshDatasource];
}
- (void)initUserinterface {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 64, CGRectGetWidth(self.view.bounds) - 40, CGRectGetHeight(self.view.bounds) / 2 + 50) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor yellowColor];
    self.tableView.layer.cornerRadius = 50;
    self.tableView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.tableView.clipsToBounds = YES;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ClockTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:identifier];
    [self.view addSubview:self.tableView];
    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.addButton.frame = CGRectMake(CGRectGetWidth(self.view.bounds) - 45, 30, 30, 30);
    [self.addButton addTarget:self action:@selector(addClock:) forControlEvents:UIControlEventTouchUpInside];
    self.addButton.layer.cornerRadius = 50;
    [self.addButton setBackgroundImage:[UIImage imageNamed:@"加号"] forState:UIControlStateNormal];
    self.addButton.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.addButton];
    //asd
    
}

- (void)addClock:(UIButton *)sender {
    [self.addVC getCellContent:^(NSMutableArray *content) {
        self.dataSource = [content mutableCopy];
        [self.tableView reloadData];
    }];
    if (self.addVC) {
    [self.navigationController pushViewController:self.addVC animated:YES];
    }
    

}

- (AddNewViewController *)addVC {
    if (!_addVC) {
        _addVC = [[AddNewViewController alloc] init];
    }
    return _addVC;
}

#pragma mark -- UITableViewDataSource,UITabBleViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ClockTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.alpha = 0.9;
    cell.contentDic = self.dataSource[indexPath.row];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
    ClockSettingViewController *settingVC = [[ClockSettingViewController alloc] init];
//    [settingVC getClockTime:^(NSString *clockTime) {
//        NSDictionary *timeDic = [NSDictionary dictionaryWithObject:clockTime forKey:@"time"];
//        [self.dataSource addObject:timeDic];
//        NSLog(@"self.datesource = %@",self.dataSource);
//    }];
    settingVC.settingDataSource = self.dataSource;
    settingVC.index = indexPath.row;
    [self.navigationController pushViewController:settingVC animated:YES];
    
    [settingVC getTheCellContent:^(NSMutableArray *content) {
        self.dataSource = [content mutableCopy];
        [self.tableView reloadData];
    }];
}
#pragma mark -- lifecycle
- (void)viewWillDisappear:(BOOL)animated {
}
@end
